package com.ljkj.cordial.chat.dao;

import android.text.TextUtils;

import com.lanjiu.lib.utils.CollectionUtils;
import com.ljkj.cordial.chat.R;
import com.nic.view.util.AndroidFileUtil;

import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Property;

import java.io.File;
import java.util.List;

/**
 * 文件类型
 */
@Entity(nameInDb = "AlbumTableBean")
public class AlbumTableBean {
    //    1TYPE_IMAGE, 2TYPE_VIDEO,3TYPE_EXTRA_FILE,4TYPE_RECORD_FILE,5TYPE_AVATAR
    public final static int TYPE_NONE = 0;
    public final static int TYPE_IMAGE = 1;
    public final static int TYPE_VIDEO = 2;
    public final static int TYPE_EXTRA_FILE = 3;
    public final static int TYPE_RECORD_FILE = 4;
    public final static int TYPE_AVATAR = 5;
    @Id(autoincrement = true)
    private Long id;
    /**
     * File path.
     */
    @Property(nameInDb = "PATH")
    private String mPath;
    /**
     * Folder mName.
     */
    @Property(nameInDb = "BUCKET_NAME")
    private String mBucketName;
    /**
     * File mime type.
     */
    @Property(nameInDb = "MINE_TYPE")
    private String mMimeType;
    /**
     * Add date.
     */
    @Property(nameInDb = "ADD_TIME")
    private long mAddDate;
    /**
     * Size.
     */
    @Property(nameInDb = "SIZE")
    private long mSize;
    /**
     * Duration.
     */
    @Property(nameInDb = "DURATION")
    private long mDuration;
    /**
     * Thumb path.
     */
    @Property(nameInDb = "THUMB_PATH")
    private String mThumbPath;
    /**
     * MediaType.
     * 1TYPE_IMAGE, 2TYPE_VIDEO,3TYPE_EXTRA_FILE,4TYPE_RECORD_FILE,5TYPE_AVATAR
     */
    @Property(nameInDb = "MEDIA_TYPE")
    private int mMediaType;
    /**
     * 网络地址
     */
//    @Unique
    @Property(nameInDb = "URL")
    private String mUrl;

    @Generated(hash = 387830798)
    public AlbumTableBean(Long id, String mPath, String mBucketName,
                          String mMimeType, long mAddDate, long mSize, long mDuration,
                          String mThumbPath, int mMediaType, String mUrl) {
        this.id = id;
        this.mPath = mPath;
        this.mBucketName = mBucketName;
        this.mMimeType = mMimeType;
        this.mAddDate = mAddDate;
        this.mSize = mSize;
        this.mDuration = mDuration;
        this.mThumbPath = mThumbPath;
        this.mMediaType = mMediaType;
        this.mUrl = mUrl;
    }

    @Generated(hash = 459384517)
    public AlbumTableBean() {
    }

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getMPath() {
        return this.mPath;
    }

    public void setMPath(String mPath) {
        this.mPath = mPath;
    }

    public String getMBucketName() {
        return this.mBucketName;
    }

    public void setMBucketName(String mBucketName) {
        this.mBucketName = mBucketName;
    }

    public String getMMimeType() {
        return this.mMimeType;
    }

    public void setMMimeType(String mMimeType) {
        this.mMimeType = mMimeType;
    }

    public long getMAddDate() {
        return this.mAddDate;
    }

    public void setMAddDate(long mAddDate) {
        this.mAddDate = mAddDate;
    }

    public long getMSize() {
        return this.mSize;
    }

    public void setMSize(long mSize) {
        this.mSize = mSize;
    }

    public long getMDuration() {
        return this.mDuration;
    }

    public void setMDuration(long mDuration) {
        this.mDuration = mDuration;
    }

    public String getMThumbPath() {
        return this.mThumbPath;
    }

    public void setMThumbPath(String mThumbPath) {
        this.mThumbPath = mThumbPath;
    }

    public int getMMediaType() {
        return this.mMediaType;
    }

    public void setMMediaType(int mMediaType) {
        this.mMediaType = mMediaType;
    }

    public String getMUrl() {
        return this.mUrl;
    }

    public void setMUrl(String mUrl) {
        this.mUrl = mUrl;
    }

    public static AlbumTableBean queryByPath(String path) {
        List<AlbumTableBean> list = DbController.getInstance().getRead().getAlbumTableBeanDao().queryBuilder().where(AlbumTableBeanDao.Properties.MPath.eq(path)).list();
        if (!CollectionUtils.isEmpty(list)) {
            return list.get(0);
        }
        return null;

    }

    public static Long queryIdByPath(String path) {
        List<AlbumTableBean> list = DbController.getInstance().getRead().getAlbumTableBeanDao().queryBuilder().where(AlbumTableBeanDao.Properties.MPath.eq(path)).list();
        if (!CollectionUtils.isEmpty(list)) {
            return list.get(0).getId();
        }
        return 0L;
    }

    public static AlbumTableBean queryByUrl(String url) {
        return DbController.getInstance().getRead().getAlbumTableBeanDao().queryBuilder().where(AlbumTableBeanDao.Properties.MUrl.eq(url)).unique();
    }

    public static Long queryIdByUrl(String url) {
        List<AlbumTableBean> list = DbController.getInstance().getRead().getAlbumTableBeanDao().queryBuilder().where(AlbumTableBeanDao.Properties.MUrl.eq(url)).list();
        if (!CollectionUtils.isEmpty(list)) {
            return list.get(0).getId();
        }
        return 0L;
    }

    public static AlbumTableBean queryById(Long id) {
        if (id <= 0) {
            return null;
        }
        try {
            return DbController.getInstance().getRead().getAlbumTableBeanDao().loadByRowId(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static AlbumTableBean queryRowByUrlNotNull(String url) {
        AlbumTableBean u = queryByUrl(url);
        return u == null ? new AlbumTableBean() : u;
    }

    public static AlbumTableBean queryRowByPathNotNull(String path) {
        AlbumTableBean u = queryByPath(path);
        return u == null ? new AlbumTableBean() : u;
    }

    public static Long insert(AlbumTableBean albumTable) {
        DbController.getInstance().getWrite().getAlbumTableBeanDao().insertOrReplace(albumTable);
        return albumTable.getId();
    }

    private static void deleteFile(String orgPath, String thumbPath) {
        if (!TextUtils.isEmpty(orgPath)) {
            File file = new File(orgPath);
            if (file.exists()) {
                file.delete();
            }
        }
        if (!TextUtils.isEmpty(thumbPath)) {
            File file = new File(thumbPath);
            if (file.exists()) {
                file.delete();
            }
        }

    }

    public static List<AlbumTableBean> query(int mMediaType) {
        return DbController.getInstance().getRead().getAlbumTableBeanDao().queryBuilder().where(AlbumTableBeanDao.Properties.MMediaType.eq(mMediaType)).list();

    }

    public static void delete(Long id) {
        DbController.getInstance().getWrite().getAlbumTableBeanDao().deleteByKey(id);
    }

    public static int getFileType(String path) {
        if (TextUtils.isEmpty(path)) {
            return AlbumTableBean.TYPE_NONE;
        }
        File file = new File(path);
        if (file.exists()) {
            String type = AndroidFileUtil.getMimeType(file);
            if (AndroidFileUtil.MimeType.DOC.equals(type) || AndroidFileUtil.MimeType.DOC.equals(type)
                    || AndroidFileUtil.MimeType.XLS.equals(type) || AndroidFileUtil.MimeType.XLSX.equals(type)
                    || AndroidFileUtil.MimeType.PPT.equals(type) || AndroidFileUtil.MimeType.PPTX.equals(type)
                    || AndroidFileUtil.MimeType.TEXT.equals(type)
                    || AndroidFileUtil.MimeType.APK.equals(type)
                    || AndroidFileUtil.MimeType.zip.equals(type)
                    || AndroidFileUtil.MimeType.PDF.equals(type))
                return AlbumTableBean.TYPE_EXTRA_FILE;
            else if (AndroidFileUtil.MimeType.JPEG.equals(type)
                    || AndroidFileUtil.MimeType.PNG.equals(type) || AndroidFileUtil.MimeType.GIF.equals(type)) {
                return AlbumTableBean.TYPE_IMAGE;
            } else if (AndroidFileUtil.MimeType.M3U8.equals(type) || AndroidFileUtil.MimeType.MP4.equals(type)) {
                return AlbumTableBean.TYPE_VIDEO;
            } else if (AndroidFileUtil.MimeType.mid_midi_kar.equals(type) || AndroidFileUtil.MimeType.ogg.equals(type)
                    || AndroidFileUtil.MimeType.m4a.equals(type) || AndroidFileUtil.MimeType.wav.equals(type) || path.endsWith(".pcm")
                    || AndroidFileUtil.MimeType.mp3.equals(type))
                return AlbumTableBean.TYPE_RECORD_FILE;
        }

        return AlbumTableBean.TYPE_NONE;
    }

    public static boolean isRecordFile(String path) {
        if (TextUtils.isEmpty(path)) {
            return false;
        }
        return path.endsWith(".m4a") || path.endsWith(".pcm") || path.endsWith(".wav") || path.endsWith(".amr");
    }

    public static int getMineType(boolean isLoc, String path) {
        if (TextUtils.isEmpty(path)) {
            return R.mipmap.icon_file_unknown;
        }
        String type;
        if (isLoc) {
            File file = new File(path);
            if (!file.exists()) {
                return R.mipmap.icon_file_unknown;
            }
            type = AndroidFileUtil.getMimeType(file);
        } else {
            type = AndroidFileUtil.getMimeType(path);
        }
        if (AndroidFileUtil.MimeType.DOC.equals(type) || AndroidFileUtil.MimeType.DOC.equals(type)) {
            return R.mipmap.icon_file_word;
        } else if (AndroidFileUtil.MimeType.XLS.equals(type) || AndroidFileUtil.MimeType.XLSX.equals(type)) {
            return R.mipmap.icon_file_excel;
        } else if (AndroidFileUtil.MimeType.PPT.equals(type) || AndroidFileUtil.MimeType.PPTX.equals(type)) {
            return R.mipmap.icon_file_ppt;
        } else if (AndroidFileUtil.MimeType.TEXT.equals(type)) {
            return R.mipmap.icon_file_txt;
        } else if (AndroidFileUtil.MimeType.APK.equals(type)) {
            return R.mipmap.icon_file_app;
        } else if (AndroidFileUtil.MimeType.JPEG.equals(type)
                || AndroidFileUtil.MimeType.PNG.equals(type) || AndroidFileUtil.MimeType.GIF.equals(type)) {
            return R.mipmap.icon_file_pic;
        } else if (AndroidFileUtil.MimeType.M3U8.equals(type) || AndroidFileUtil.MimeType.MP4.equals(type)) {
            return R.mipmap.icon_filev_video;
        } else if (AndroidFileUtil.MimeType.PDF.equals(type)) {
            return R.mipmap.icon_file_pdf;
        } else if (AndroidFileUtil.MimeType.mid_midi_kar.equals(type) || AndroidFileUtil.MimeType.ogg.equals(type)
                || AndroidFileUtil.MimeType.m4a.equals(type) || AndroidFileUtil.MimeType.wav.equals(type) || path.endsWith(".pcm")) {
            return R.mipmap.icon_file_recording;
        } else if (AndroidFileUtil.MimeType.mp3.equals(type)) {
            return R.mipmap.icon_file_music;
        } else if (AndroidFileUtil.MimeType.zip.equals(type)) {
            return R.mipmap.icon_file_zip;
        }
        return R.mipmap.icon_file_unknown;
    }
}
